package com.rpay.service.user.impl;

import com.google.common.collect.Lists;
import com.rpay.common.dto.contract.ContractDTO;
import com.rpay.common.dto.user.UserRippleDTO;
import com.rpay.common.util.DateUtil;
import com.rpay.dao.user.UserRippleDao;
import com.rpay.model.contract.ContractDO;
import com.rpay.model.user.UserRippleDO;
import com.rpay.service.user.IUserRippleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Title：
 * Description
 *
 * @author fxg06 on 2018/5/19
 * @version 1.0
 */
@Service
public class UserRippleServiceImpl implements IUserRippleService {
    @Autowired
    private UserRippleDao userRippleDao;

    @Override
    public List<ContractDTO> listContract() {
        List<ContractDTO> contractList = Lists.newArrayList();
        List<ContractDO> list = userRippleDao.selectContract();
        if (!CollectionUtils.isEmpty(list)) {
            for (ContractDO contractDO : list) {
                ContractDTO contractDTO = new ContractDTO();
                contractDTO.setId(contractDO.getId());
                contractDTO.setName(contractDO.getName());
                contractDTO.setDesc(contractDO.getDesc());
                contractDTO.setEnName(contractDO.getEnName());
                contractList.add(contractDTO);
            }
        }
        return contractList;
    }

    @Override
    public List<UserRippleDTO> listUserRipple(Integer userId) {
        List<UserRippleDO> userRippleList = userRippleDao.selectUserRipples(userId);
        List<UserRippleDTO> list = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(userRippleList)) {
            for (UserRippleDO userRippleDO : userRippleList) {
                list.add(convertDOToDTO(userRippleDO));
            }
        }
        return list;
    }

    @Override
    public boolean saveUserRipple(UserRippleDTO userRippleDTO) {
        UserRippleDO userRippleDO = new UserRippleDO();
        userRippleDO.setUserId(userRippleDTO.getUserId());
        userRippleDO.setContractId(userRippleDTO.getContractId());
        userRippleDO.setInvestAmount(userRippleDTO.getInvestAmount());
        if (userRippleDO.getContractId() == 1) {
            userRippleDO.setExtractAmount(userRippleDO.getInvestAmount().multiply(new BigDecimal("0.5")));
            userRippleDO.setExtractDate(DateUtil.addMonths(new Date(), 3));
        } else if (userRippleDO.getContractId() == 2) {
            userRippleDO.setExtractAmount(userRippleDO.getInvestAmount().multiply(new BigDecimal("0.7")));
            userRippleDO.setExtractDate(DateUtil.addMonths(new Date(), 6));
        }  else {
            userRippleDO.setExtractAmount(userRippleDO.getInvestAmount());
            userRippleDO.setExtractDate(DateUtil.addMonths(new Date(), 12));
        }
        userRippleDO.setPrice(userRippleDTO.getPrice());
        userRippleDO.setRpayAmount(userRippleDTO.getInvestAmount());

        userRippleDao.insert(userRippleDO);
        return false;
    }

    private UserRippleDTO convertDOToDTO(UserRippleDO userRippleDO) {
        UserRippleDTO userRippleDTO = new UserRippleDTO();
        if (userRippleDO != null) {
            userRippleDTO.setId(userRippleDO.getId());
            userRippleDTO.setUserId(userRippleDO.getUserId());
            userRippleDTO.setInvestAmount(userRippleDO.getInvestAmount());
            userRippleDTO.setPrice(userRippleDO.getPrice());
            userRippleDTO.setContractId(userRippleDO.getContractId());
            userRippleDTO.setExtractAmount(userRippleDO.getExtractAmount());
            userRippleDTO.setExtractDate(userRippleDO.getExtractDate());
            userRippleDTO.setResidueAmount(userRippleDO.getResidueAmount());
            userRippleDTO.setRpayAmount(userRippleDO.getRpayAmount());
            userRippleDTO.setExpire(false);
            //如果到期日小于当前时间，表示合约已到期
            if (userRippleDO.getExtractDate().getTime() < System.currentTimeMillis()) {
                userRippleDTO.setExpire(true);
            }
        }
        return userRippleDTO;
    }
}
